<template>
   <div class="login-container">
     <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="login-form">
       <h2 class="login-title">积云会员管理系统</h2>
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
       </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>
         <el-form-item>
          <el-button type="primary" @click="onSubmit('form')">登录</el-button>
       </el-form-item>
     </el-form>
  </div>
</template>

<script>
import loginApi from '../../api/login'
    export default {
          data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          //调用登录接口
              loginApi.login(this.form.username, this.form.password).then(response => {
              const resp = response.data;

              console.log(resp.data.token);

              if(resp.flag){
                // 通过token,在加上调用用户信息接口获取数据
                loginApi.getInfo(resp.data.token).then(response=>{
                  const respUser = response.data;
                  console.log(respUser.data)
                  if(respUser.flag){
                      
                      localStorage.setItem("adminInfo",JSON.stringify(respUser.data))
                 
                      localStorage.setItem("adminToken",resp.data.token)
                   
                      this.$router.push("hader");
                  }else{
                    this.$message({
                      duration : 1 * 1000,
                      showClose: true,
                      message: resp.message,
                      type: 'warning'
                    });
                  }
                })
              }else{
                this.$message({
                  duration : 1 * 1000,
                  showClose: true,
                  message: resp.message,
                  type: 'warning'
                });
                return false;
              }
            })
        } else {
          console.log("error submit");
          return false;
           }
         });
        }
      }
    }
</script>

<style >
.login-form {
  width: 350px;
  background: rgba(255, 255, 255, 0.8);
  padding: 40px;
  margin: 160px auto;
  border-radius: 20px;
}
.login-title {
  text-align: center;
  color: #303133;
}
.login-container {
  width: 100%;
  height: 100%;
  position: absolute;
  background: url("../../assets/login_bg.jpg");
  overflow: hidden;
}
</style>